package com.qst.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qst.pojo.Employee;
import com.qst.pojo.Equipment;
import com.qst.pojo.Result;
import com.qst.pojo.User;
import com.qst.query.EquipmentQuery;
import com.qst.query.MemberQuery;
import com.qst.service.EquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Random;

/**
 * @ClassNmae: EquipmentController
 * @Author: 邓嘉顺
 * @Description:
 */
@Controller
@RequestMapping("/equipment")
public class EquipmentController {
    @Autowired
    EquipmentService equipmentService;

    @RequestMapping("/ToEquipment")
    public String ToEquipment(){
        return "equipmentInfo";
    }
    @RequestMapping("/ToEquipmentInfo")
    @ResponseBody
    public Result ToEquipmentInfo(Model model, @ModelAttribute EquipmentQuery equipmentQuery) throws JsonProcessingException {
        PageHelper.startPage(equipmentQuery.getPage(),equipmentQuery.getLimit());
        List<Equipment> equipmentList=  equipmentService.queryAllEquipment( equipmentQuery.getEquipmentName());
        PageInfo<Equipment> pageInfo = new PageInfo<>(equipmentList);
        List<Equipment> data = pageInfo.getList();
        int count =  equipmentService.getAllCount();
        return  new Result(0,"",data,count);
    }

    @RequestMapping("/uploadFile")
    @ResponseBody
//    获取上传后文件的完整路径
    public Result uploadFile(@RequestParam("file") MultipartFile upload) throws IOException {
//        获取上传文件的名称
        String fileName= upload.getOriginalFilename();//img.jsp
//       产生一个新的文件名，时间戳+原先的文件后缀,substring()截取,一个参数从这个位置到最后
//        index of  last index of,找到目标字符出现的位置
        String newFileName=System.currentTimeMillis()+fileName.substring(fileName.lastIndexOf("."));
        String path="E:\\SpringFramwork\\SSM02\\target\\SSM02\\upload";
        File file =new File(path);
//        如果不存在就创建这个路径
        if(!file.exists()){
            file.mkdir();
        }
//        文件夹的路径+文件的名称
//        upload.transferTo(new File(path+"\\"+newFileName));
//        将文件上传到次目的地,完成上传
        upload.transferTo(new File(path+File.separator+newFileName));
//        获取文件名放到数据库中
        String str="http://localhost:8080/SSM02_war_exploded/upload/";
        String imgStr=str+newFileName;
//        equipmentService.addEquipment02(imgStr);
        return new Result(200,imgStr);
//        return fileName;
    }

    @RequestMapping("/ToAddEquipment")
    public String addEquipment(){
        return "addEquipment";
    }


    @RequestMapping("/addEquipment")
    @ResponseBody
    public Result addEquipment(Equipment equipment){
        Result result = equipmentService.addEquipment(equipment);
        return result;
    }

    @RequestMapping("/delEquipment")
    @ResponseBody
    public Result delMember(@RequestParam("ids[]") Integer[] ids){
        Result result = equipmentService.delBatch(ids);
        return result;
    }
    @RequestMapping("/EditEquipment")
    @ResponseBody
    public Result EditMember(@RequestParam("id") Integer id, @RequestParam("field") String field, @RequestParam("value") String value){
//        转换，将userAge转换为user_age
        String columnName =   field.replaceAll("[A-Z]", "_$0").toLowerCase();    // 数据库中对应的列名
        Result result=equipmentService.EditEquipment(id,columnName,value);
        return result;
    }
}
